interface JQuery {
    /**
    * 初始化编辑器
    */
    initEditor(): JQuery;
    /**
     *重置编辑器
     */
    resetEditor(): JQuery;
    /**
     * 设置对象值到编辑器
     * @param {Object} model
     */
    setToEditor(model: Object): JQuery;
    /**
     * 从编辑器取值到对象
     * @param {Object} model
     */
    editorToModel(model: Object): JQuery;
}


$.fn.initEditor = function () {
    $(this).find(".myeditor").each((i, o) => {
        try {
            var editor = new baidu.editor.ui.Editor({ initialframewidth: "100%" });
            editor.render($(o).prop("id"));
            $(o).data("editor", editor);
        }
        catch (e) {
            console.log("编辑器初始化失败");
        }
    });
    return this;
}

///重置编辑器
$.fn.resetEditor = function () {
    try {
        $(this).find(".myeditor").each((i, o) => {
            var editor: baidu.editor.ui.Editor = $(o).data("editor");
            editor.ready(() => {
                editor.setContent('');
            });
        })
    }
    catch (e) {
        console.log("重置编辑器失败");
    }
    return this;
}


///从编辑器取值到实体
$.fn.editorToModel = function (model: any) {
    try {
        $(this).find(".myeditor").each((i, o) => {
            var editor = $(o).data("editor");
            editor.ready(() => { model[$(o).data("item")] = editor.getContent(); });
        });
    }
    catch (e) {
        console.log("从编辑器取值失败");
    }
    return model;
}

///设置值到编辑器
$.fn.setToEditor = function (model: any) {
    try {
        $(this).find(".myeditor").each((i, o) => {
            var $obj = $(o);
            var editor = $obj.data("editor");
            editor.ready(() => { editor.setContent(model[$obj.data("item")]); });
        });
    }
    catch (e) {
        console.log("设置编辑器内容失败");
    }
    return this;
}



$("body")
    .on("after.ResetForm", function (sender, form) {
        form.initEditor();
    })
    .on("after.InitForm", function (sender, form) {
        form.resetEditor();
    })

